package com.bbg.amyam.web.util.menu;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLMenuItemImporter {

	ArrayList<MenuItem> reportItems = new ArrayList<MenuItem>();

	public XMLMenuItemImporter(String menuName) {
		try {
			populateMenuItems(menuName);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void populateMenuItems(String menuName)
			throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		InputStream stream = XMLMenuItemImporter.class.getResourceAsStream("/"
				+ menuName);
		Document document = docBuilder.parse(stream);
		Element ele = document.getDocumentElement();
		parse(ele);
	}

	private void parse(Element ele) {

		NodeList nodes = ele.getChildNodes();
		for (int index = 0; index < nodes.getLength(); index++) {
			Node node = nodes.item(index);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				MenuItem item = proceNode(node);
				reportItems.add(item);
			}
		}

	}

	private MenuItem proceNode(Node node) {
		MenuItem item = new MenuItem();
		NamedNodeMap attri = node.getAttributes();
		// <menuitem id="home" target="home/index" role="" />
		Node attNode = attri.getNamedItem("id");
		String value = attNode.getNodeValue();
		item.setId(value);
		attNode = attri.getNamedItem("target");
		value = attNode.getNodeValue();
		item.setTarget(value);
		attNode = attri.getNamedItem("role");
		value = attNode.getNodeValue();
		item.setRole(value);
		attNode = attri.getNamedItem("default");
		if (attNode != null) {
			value = attNode.getNodeValue();
			if ("true".equalsIgnoreCase(value)) {
				item.setDefalutMenuItem(true);
			} else {
				item.setDefalutMenuItem(false);
			}
		}

		attNode = attri.getNamedItem("include");
		if (attNode != null) {
			value = attNode.getNodeValue();
			item.setInclude(value);
		}

		attNode = attri.getNamedItem("category");
		if (attNode != null) {
			value = attNode.getNodeValue();
			item.setCategory(value);
		}
		
		attNode = attri.getNamedItem("domain");
		if (attNode != null) {
			value = attNode.getNodeValue();
			item.setDomain(value);
		}
		
		attNode = attri.getNamedItem("type");
		if (attNode != null) {
			value = attNode.getNodeValue();
			item.setType(value);
		}
		
		
		attNode = attri.getNamedItem("controllertemplate");
		if (attNode != null) {
			value = attNode.getNodeValue();
			item.setControllertemplate(value);
		}
		
		attNode = attri.getNamedItem("execludedDomainFields");
		if (attNode != null) {
			value = attNode.getNodeValue();
			String[] fields = StringUtils.split(value, ',');
			for(String field:fields){
				item.addExeculdeDomainFiled(field);
			}
		}

		NodeList children = node.getChildNodes();
		for (int index = 0; index < children.getLength(); index++) {
			Node child = children.item(index);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				MenuItem childMenu = proceNode(child);
				childMenu.setParent(item);
				item.addChild(childMenu);
			}
		}

		return item;
	}

	public ArrayList<MenuItem> getReportItems() {
		return reportItems;
	}

}
